<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>服务部署</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <style>
        .log-window {
            background-color: #1e1e1e;
            color: #d4d4d4;
            font-family: monospace;
            padding: 10px;
            height: 400px;
            overflow-y: auto;
            white-space: pre-wrap;
            word-wrap: break-word;
        }
        .log-info { color: #569cd6; }
        .log-error { color: #f14c4c; }
        .log-success { color: #6a9955; }
        .log-line { margin: 0; line-height: 1.5; }
    </style>
</head>
<body>
    <div class="container mt-4">
        <h2>服务部署</h2>
        <div class="table-responsive">
            <table class="table table-striped">
                <thead>
                    <tr>
                        <th>服务包名</th>
                        <th>状态</th>
                        <th>操作</th>
                    </tr>
                </thead>
                <tbody id="serviceList">
                    <!-- 服务列表将通过JavaScript动态填充 -->
                </tbody>
            </table>
        </div>
    </div>

    <!-- 部署日志弹窗 -->
    <div class="modal fade" id="deploymentLogModal" tabindex="-1">
        <div class="modal-dialog modal-lg">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title">部署日志</h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                </div>
                <div class="modal-body">
                    <div class="log-window" id="logWindow"></div>
                </div>
            </div>
        </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
    <script>
        // 获取服务列表
        function fetchServiceList() {
            fetch('/deployment/list?cangKuUuid=your-uuid-here')
                .then(response => response.json())
                .then(data => {
                    if (data.code === 200) {
                        const serviceList = document.getElementById('serviceList');
                        serviceList.innerHTML = '';
                        data.data.forEach(service => {
                            const row = document.createElement('tr');
                            row.innerHTML = `
                                <td>${service.servicePackageName}</td>
                                <td>${service.serviceStatus === '1' ? '运行中' : '已停止'}</td>
                                <td>
                                    <button class="btn btn-primary btn-sm" onclick="deployService('${service.servicePackageName}')" ${service.serviceStatus === '1' ? 'disabled' : ''}>
                                        部署
                                    </button>
                                    <button class="btn btn-danger btn-sm" onclick="stopService('${service.servicePackageName}')" ${service.serviceStatus === '0' ? 'disabled' : ''}>
                                        停止
                                    </button>
                                </td>
                            `;
                            serviceList.appendChild(row);
                        });
                    }
                });
        }

        // 部署服务
        function deployService(jarName) {
            const logWindow = document.getElementById('logWindow');
            logWindow.innerHTML = '';
            
            // 显示日志弹窗
            const modal = new bootstrap.Modal(document.getElementById('deploymentLogModal'));
            modal.show();

            // 创建EventSource连接
            const eventSource = new EventSource(`/deployment/deploy?cangKuUuid=your-uuid-here&jarName=${jarName}`);

            eventSource.onmessage = function(event) {
                const data = JSON.parse(event.data);
                const logLine = document.createElement('p');
                logLine.className = `log-line log-${data.type.toLowerCase()}`;
                logLine.textContent = data.message;
                logWindow.appendChild(logLine);
                logWindow.scrollTop = logWindow.scrollHeight;

                // 如果收到成功或错误消息，关闭连接
                if (data.type === 'SUCCESS' || data.type === 'ERROR') {
                    eventSource.close();
                    setTimeout(fetchServiceList, 1000); // 刷新服务列表
                }
            };

            eventSource.onerror = function() {
                eventSource.close();
                const logLine = document.createElement('p');
                logLine.className = 'log-line log-error';
                logLine.textContent = '连接已断开';
                logWindow.appendChild(logLine);
            };

            // 当弹窗关闭时，关闭EventSource连接
            document.getElementById('deploymentLogModal').addEventListener('hidden.bs.modal', function () {
                eventSource.close();
            });
        }

        // 停止服务
        function stopService(jarName) {
            if (confirm('确定要停止该服务吗？')) {
                fetch(`/deployment/stop?cangKuUuid=your-uuid-here&jarName=${jarName}`)
                    .then(response => response.json())
                    .then(data => {
                        alert(data.msg);
                        if (data.code === 200) {
                            fetchServiceList();
                        }
                    });
            }
        }

        // 页面加载时获取服务列表
        document.addEventListener('DOMContentLoaded', fetchServiceList);
    </script>
</body>
</html> 